package com.caiyi.financial.nirvana.sms.service;

import com.alibaba.fastjson.JSON;
import com.caiyi.financial.nirvana.sms.entity.SmsGateway;
import com.caiyi.financial.nirvana.sms.entity.SmsUser;
import com.caiyi.financial.nirvana.sms.enums.ReturnCodeEnum;
import com.caiyi.financial.nirvana.sms.jedis.JedisKey;
import com.caiyi.financial.nirvana.sms.jedis.JedisService;
import com.caiyi.financial.nirvana.sms.param.QueryReportParam;
import com.caiyi.financial.nirvana.sms.queue.NotifyQueueEntity;
import com.caiyi.financial.nirvana.sms.util.CommUtil;
import com.caiyi.financial.nirvana.sms.util.SignUtil;
import com.youyu.common.utils.YyAssert;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 接收第三方网关状态报告
 */
@Service
public class SmsNotifyService {

    private static Logger logger = LoggerFactory.getLogger(SmsNotifyService.class);

    public String notifyStatus(Map<String, String> param, Integer gatewayId, String json) {
        logger.info("notifyStatus param : {}, gatewayId : {}, json", param, gatewayId, json);
        String key = String.format(JedisKey.SMS_GATEWAY, gatewayId);
        String gatewayJson = JedisService.get(key);
        if (StringUtils.isBlank(gatewayJson)) {
            return "ERROR";
        }
        SmsGateway gateway = JSON.parseObject(gatewayJson, SmsGateway.class);
        NotifyQueueEntity entity = new NotifyQueueEntity();
        entity.setGatewayId(gatewayId);
        entity.setParam(json);
        if (param.size() > 0) {
            entity.setParam(JSON.toJSONString(param));
        }
        JedisService.lpush(JedisKey.SMS_QUEUE_NOTIFY_STATUS, JSON.toJSONString(entity));
        logger.info("notifyStatus param : {}, gatewayId : {}, result : {}", param, gatewayId, gateway.getNotifyResult());
        return gateway.getNotifyResult();
    }

    /**
     * 主动获取状态报告接口，一次支持100条
     *
     * @param param
     * @return
     */
    public List queryReport(QueryReportParam param, HttpServletRequest request) {
        logger.info("SmsNotifyService queryReport param : {}", param);
        //获取用户信息
        String userKey = String.format(JedisKey.SMS_USER, param.getAccount());
        String userJson = JedisService.get(userKey);
        if (StringUtils.isBlank(userJson)) {
            YyAssert.isFalse(true, ReturnCodeEnum.ACCOUNT_ERROR);
        }
        SmsUser user = JSON.parseObject(userJson, SmsUser.class);
        //签名验证
        if (!SignUtil.verifySign(param, user.getPassword(), param.getSign())) {
            YyAssert.isFalse(true, ReturnCodeEnum.REQUEST_SIGN_ERROR);
        }
        String key = String.format(JedisKey.SMS_QUEUE_REPORT_LIST_ACCOUNT, param.getAccount());
        String cliIp = CommUtil.getClientIp(request);

        List list = new ArrayList();
        long listLength = JedisService.llen(key);
        long length = listLength > 100 ? 100 : listLength;
        for (long i = 0; i < length; i++) {
            String entity = JedisService.lpop(key);
            if (StringUtils.isNotBlank(entity)) {
                list.add(JSON.parse(entity));
            }
        }
        logger.info("queryReport account {}, list : {}, ip : {}", param.getAccount(), JSON.toJSONString(list), cliIp);
        return list;
    }

}
